package com.monkey.tensquare.gathering.service.impl;

import com.monkey.tensquare.gathering.entity.Gathering;
import com.monkey.tensquare.gathering.mapper.GatheringMapper;
import com.monkey.tensquare.gathering.service.GatheringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.List;

@Service
public class GatheringServiceImpl implements GatheringService {
    @Autowired
    private GatheringMapper gatheringMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(Gathering gathering) {
        gathering.setId(idWorker.nextId() + "");
        gatheringMapper.save(gathering);
    }

    @Override
    public List<Gathering> findAll() {
        return gatheringMapper.findAll();
    }

    @Cacheable(value = "gathing", key = "#gatheringId")
    @Override
    public Gathering findById(String gatheringId) {
        System.out.println("从数据库中取");
        return gatheringMapper.findById(gatheringId).get();
    }

    @CacheEvict(value = "gathing", key = "#gatheringId")
    @Override
    public void update(String gatheringId, Gathering gathering) {
        gathering.setId(gatheringId);
        gatheringMapper.save(gathering);
    }

    @CacheEvict(value = "gathing", key = "#gatheringId")
    @Override
    public void delete(String gatheringId) {
        gatheringMapper.deleteById(gatheringId);
    }
}
